#include "nwbaseconnection.h"

namespace NetworkUtil {

#if 0

#include "asio.hpp"

std::shared_ptr<asio::io_context> m_sp4ObjIOCtx = nullptr;
std::shared_ptr<asio::ip::udp::socket> m_sp4ObjSocket = nullptr;
asio::ip::udp::resolver::results_type m_objSrvEndPoints;

void asyncRead(std::function<void(const std::string& strDataBuf)> pfnCallback)
{
    if (m_sp4ObjSocket.get())
    {
        char *pBuffer = new (std::nothrow) char[1024];
        if (!pBuffer)
        {
            return;
        }

        memset(pBuffer, 0, 1024);
        m_sp4ObjSocket->async_receive_from(asio::buffer(pBuffer, static_cast<std::size_t>(m_stConnCfgInfo.ui32BufMaxSize))
                                           , m_objSenderEndPoint
                                           , [&pBuffer, &pfnCallback](std::error_code ec, std::size_t bytes_recvd)
        {
            if (!ec && bytes_recvd > 0)
            {
                if (pfnCallback)
                {
                    std::string strDataInfo(pBuffer, bytes_recvd);
                    pfnCallback(strDataInfo);
                }
            }

            if (pBuffer)
            {
                delete[] pBuffer;
                pBuffer = nullptr;
            }

            return;
        });
    }

    return;
}

void asyncWrite(const std::string& strDataBuf)
{
    if (m_sp4ObjSocket.get())
    {
        std::error_code errCode;
        std::size_t sendByte;

        m_sp4ObjSocket->async_send_to(asio::buffer(strDataBuf.data(), strDataBuf.size())
                                      , *m_objSrvEndPoints.begin()
                                      , [&errCode, &sendByte, this](std::error_code ec, std::size_t bytes_sent)
        {
            (void)this;
            errCode = ec;
            sendByte = bytes_sent;
            return;
        });
    }

    return;
}
#endif

BaseConnection::BaseConnection(const ConnectionConfigInfo& stConnCfgInfo)
    : m_stConnCfgInfo(stConnCfgInfo)
{

}

BaseConnection::~BaseConnection()
{

}

void BaseConnection::setConnCfgInfo(const ConnectionConfigInfo& stConnCfgInfo)
{
    m_stConnCfgInfo = stConnCfgInfo;
    return;
}

std::string BaseConnection::remoteInfo(const asio::ip::udp::endpoint& objEndPoint)
{
    std::string strInfo = objEndPoint.address().to_string() + ":" + std::to_string(objEndPoint.port());
    return strInfo;
}

std::string BaseConnection::remoteInfo(const asio::ip::tcp::endpoint& objEndPoint)
{
    std::string strInfo = objEndPoint.address().to_string() + ":" + std::to_string(objEndPoint.port());
    return strInfo;
}


} // namespace NetworkUtil
